﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NewsletterCreator
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DatedNewsletterItem runat=server></{0}:DatedNewsletterItem>")]
    public class DatedNewsletterItem : HtmlableControl
    {
        private Label lblTitle = new Label();
        private Calendar calStart = new Calendar();
        private Calendar calEnd = new Calendar();

        private string title ="default";
        private DataTable datasource;
        private int datasourceDateColumn;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Title
        {
            get
            {
                return title;
            }

            set
            {
                title = value;
            }
        }

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        [Localizable(true)]
        public DataTable Datasource
        {
            get
            {
                return datasource;
            }

            set
            {
                datasource = value;
            }
        }

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("The index of the column in the datasource that is the date or start date.")]
        [Localizable(true)]
        public int DatasourceDateColumn
        {
            get
            {
                return datasourceDateColumn;
            }

            set
            {
                datasourceDateColumn = value;
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
        }

        protected override void CreateChildControls()
        {
            lblTitle.Text = "<b>" + title + "</b>";
            Label lblStart = new Label();
            lblStart.Text = "Display items dated no earlier than: ";
            Panel pnlStart = new Panel();

            pnlStart.Controls.Add(lblStart);
            pnlStart.Controls.Add(calStart);

            Label lblEnd = new Label();
            lblEnd.Text = "Display items dated no later than: ";
            Panel pnlEnd = new Panel();

            pnlEnd.Controls.Add(lblEnd);
            pnlEnd.Controls.Add(calEnd);

            Panel pnlLayout = new Panel();
            pnlLayout.BorderWidth = 1;

            pnlLayout.Controls.Add(lblTitle);
            pnlLayout.Controls.Add(new LiteralControl("<BR />"));
            pnlLayout.Controls.Add(pnlStart);
            pnlLayout.Controls.Add(pnlEnd);

            Controls.Clear();
            Controls.Add(pnlLayout);                       
            
        }

        public override string toHTML()
        {
            string str = "<h4>" + title + "</h4>";
            
            str += "<ul>";
            for (int i = 0; i < datasource.Rows.Count; i++)
            {
                DateTime eventDate = Convert.ToDateTime(datasource.Rows[i].ItemArray[datasourceDateColumn]);
                if (eventDate > calStart.SelectedDate && eventDate < calEnd.SelectedDate)
                {
                    str += "<li><b>" + datasource.Rows[i].ItemArray[0] + "</b><br />";
                                        
                    for (int j = 1; j < +datasource.Rows[i].ItemArray.Length; j++)
                        str += datasource.Rows[i].ItemArray[j] + "<br />";
                    str += "</li>";
                }
            }
            str += "</ul>";

            return str;
        }
    }
}
